package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * The type Common controller.
 */
@RestController
@Slf4j
@RequestMapping("/admin/common")
@RequiredArgsConstructor
public class CommonController {
    private final AliOssUtil aliOssUtil;

    /**
     * 文件上传
     *
     * @param file the file
     * @return the result
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){

        try {
            byte[] bytes = file.getBytes();
            String uuid = UUID.randomUUID().toString();
            String originalFilename = file.getOriginalFilename();
            String newFileName = uuid + originalFilename.substring(originalFilename.lastIndexOf("."));
            String url = aliOssUtil.upload(bytes, newFileName);
            return Result.success(url);
        } catch (IOException e) {
            log.info(e.getMessage(),e);
        }
        return Result.error("文件上传失败");

    }


}
